<?php
namespace app\common\model;
use think\Request;

/**
 * 门店模型
 */
class Shop extends BaseModel
{
    protected $name = 'shop';
	protected $append = ['shop_logo'];
	
	/**
     * 格式化页面数据（读取的时候对数据转换）
     */
    public function getFoodModeAttr($json)
    {
        return json_decode($json, true);
    }
	

    /**
     * 自动转换data为json格式(修改器，保存de时候操作)
     */
    public function setFoodModeAttr($value)
    {
        return json_encode($value ?:['1']);
    }
	
	/**
     * 门店头像
     */
    public function getShopLogoAttr($value)
    {
        $wxapp = Wxapp::find();
		if(empty($wxapp['head_img'])){
			return api_url().'assets/store/img/shop_logo.png';
		}
		return $wxapp['head_img'];
    }
	
	/**
     * 关联门店LOGO
     */
    public function logo()
    {
		return $this->hasOne('uploadFile', 'file_id', 'logo_image_id');		
    }
	
    /**
     * 身份
     */
    public function getStatusAttr($value)
    {
        $status = ['歇业中', '营业中'];
        return ['text' => $status[$value], 'value' => $value];
    }
	
	/**
     * 自提核销
     */
    public function getIsCheckAttr($value)
    {
        $status = ['关闭', '开启'];
        return ['text' => $status[$value], 'value' => $value];
    }

    /**
     * 获取列表
     */
    public function getList($location='')
    {
        // 排序规则
        $sort = [];
        $sort = ['sort', 'shop_id' => 'desc'];
        // 执行查询
        $list = $this->with(['logo'])
            ->order($sort)
            ->paginate(15, false, ['query' => Request::instance()->request()]);
		//是否计算距离
		if($location){
			//获取派送设置
			$delivery =(new Setting)->getItem('delivery');
			for($n=0;$n<sizeof($list);$n++){
				$result = getDistance($location,$list[$n]['coordinate']);
				if($result){
					$distance = $result[0]['distance'];//获取距离计算结果
					$list[$n]['range'] = 0; //1为超出派送范围
					if($delivery['delivery_range'] < $distance){
						$list[$n]['range'] = 1; //1为超出派送范围
					}
					if($distance>=1000){
						$distance = sprintf("%.2f", $result[0]['distance']/1000).'公里';
					}else{
						$distance = $result[0]['distance'].'米';
					}
					$list[$n]['location'] = $distance;
				}else{
					$list[$n]['location'] = '未知距离';
				}
			}
		}
        return $list;
    }

	/**
     * 获取详情
     */
    public static function detail($shop_id=0,$location='')
    {
		$where = [];
		 $shop_id > 0 && $where['shop_id'] = $shop_id;
        $detail = self::where($where)->with(['logo'])->find();
		//是否计算距离
		if($location){
			//获取派送设置
			$delivery =(new Setting)->getItem('delivery');
			$result = getDistance($location,$detail['coordinate']);
			if($result){
				$distance = $result[0]['distance'];//获取距离计算结果
				$detail['range'] = 0; //1为超出派送范围
				if($delivery['delivery_range'] < $distance){
					$detail['range'] = 1; //1为超出派送范围
				}
				if($distance>=1000){
					$distance = sprintf("%.2f", $result[0]['distance']/1000).'公里';
				}else{
					$distance = $result[0]['distance'].'米';
				}
				$detail['location'] = $distance;
			}else{
				$detail['location'] = '未知距离';
			}
		}
		return $detail;
    }
	
	/**
     * 获取所有 - api接口
     */
    public static function getAll($showNum=null)
    {
		$filter['status'] = 1;
		$sort = ['sort', 'shop_id' => 'desc'];
        return self::where($filter)->order($sort)->limit($showNum)->select();
    }
	
	/**
     * 新增默认门店
     */
    public function insertDefault($wxapp_id,$data)
    {
        return $this->save([
            'shop_name' => $data['shop_name'],
			'linkman' => $data['linkman'],
			'phone' => $data['phone'],
            'wxapp_id' => $wxapp_id,
        ]);
    }
}
